$(document).ready(function () {

    $("#form").validate({
        onfocusout: function (element) {
            $(element).valid();
        },
        showErrors: function (errorMap, errorList) {
            $.each(this.validElements(), function (index, element) {
                var $element = $(element);
                $element.data("title", "").removeClass("error").tooltip("destroy");
            });
            $.each(errorList, function (index, error) {
                var $element = $(error.element);
                $element.tooltip("destroy").data("title", error.message).addClass("error").tooltip();
            });
        }
    });
    
    $(document).on("submit", "#form",function(){
        
        $.post(URLBASE+'/'+CONTROL+'/contato', $(this).serialize() ,function(data) {
            if(data.ok){
                document.getElementById("form").reset();
                alert('Mensagem enviada com sucesso!');
            } else alert('Ocorreu um erro. Tente novamente mais tarde!');
        }, 'json');
        
        return false;
    });  
});